﻿using System;
using System.Text;
using System.Collections.Generic;
using Hxsoft;

namespace Hxsoft.AppV1
{
	public struct PageMessage
	{
		public const string ClientMessageKey = "__PageMessages";
		const char EncodeMessageSplitCharacter = '&';
		const char EncodeMessageItemSplitCharacter = '|';


		private bool _allowCloseDialog;
		private bool? _allowCloseWindow;
		private PageMessageType _messageType;
		private string _title;
		private string _content;
		private string _goUrl;
		private string _goText;
		private string _autoGoUrl;
		private string _autoGoText;
		private int? _autoGoDelay;

		/// <summary>
		/// 是否允许关闭对话框。
		/// </summary>
		public bool AllowCloseDialog
		{
			get { return _allowCloseDialog; }
			set { _allowCloseDialog = value; }
		}

		/// <summary>
		/// 是否允许关闭窗口。为空时将自动判断窗口是否为弹出窗口，如果是则显示关闭窗口按钮。
		/// </summary>
		public bool? AllowCloseWindow
		{
			get { return _allowCloseWindow; }
			set { _allowCloseWindow = value; }
		}

		/// <summary>
		/// 消息的类别。
		/// </summary>
		public PageMessageType MessageType
		{
			get { return _messageType; }
			set { _messageType = value; }
		}

		/// <summary>
		/// 消息框标题。
		/// </summary>
		public string Title
		{
			get { return _title; }
			set { _title = value; }
		}

		/// <summary>
		/// 消息内容。
		/// </summary>
		public string Content
		{
			get { return _content; }
			set { _content = value; }
		}

		/// <summary>
		/// 页面跳转地址。
		/// </summary>
		public string GoUrl
		{
			get { return _goUrl; }
			set { _goUrl = value; }
		}

		/// <summary>
		/// 跳转的文本。
		/// </summary>
		public string GoText
		{
			get { return _goText; }
			set { _goText = value; }
		}

		/// <summary>
		/// 自动跳转的地址。
		/// </summary>
		public string AutoGoUrl
		{
			get { return _autoGoUrl; }
			set { _autoGoUrl = value; }
		}

		/// <summary>
		/// 自动跳转的延时时间。PS:单位为毫秒。
		/// </summary>
		public int? AutoGoDelay
		{
			get { return _autoGoDelay; }
			set { _autoGoDelay = value; }
		}

		/// <summary>
		/// 自动跳转的文本。
		/// </summary>
		public string AutoGoText
		{
			get { return _autoGoText; }
			set { _autoGoText = value; }
		}


		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="messageType">消息的类型。</param>
		/// <param name="title">消息框标题。</param>
		/// <param name="content">消息内容。</param>
		public PageMessage(PageMessageType messageType, string title, string content)
			: this(true, null, messageType, title, content)
		{
			this.MessageType = messageType;
			this.Title = title;
			this.Content = content;
		}


		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="allowCloseDialog">是否允许关闭对话框。</param>
		/// <param name="allowCloseWindow">是否允许关闭窗口。为空时将自动判断窗口是否为弹出窗口，如果是则显示关闭窗口按钮。</param>
		/// <param name="messageType">消息的类型。</param>
		/// <param name="title">消息框标题。</param>
		/// <param name="content">消息内容。</param>
		public PageMessage(bool allowCloseDialog,
			bool? allowCloseWindow,
			PageMessageType messageType,
			string title,
			string content)
			: this(allowCloseDialog, allowCloseWindow, messageType, title, content, null, null)
		{
		}


		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="messageType">消息的类型。</param>
		/// <param name="title">消息框标题。</param>
		/// <param name="content">消息内容。</param>
		/// <param name="goUrl">页面跳转地址。</param>
		/// <param name="goText">跳转的文本。</param>
		public PageMessage(PageMessageType messageType,
			string title,
			string content,
			string goUrl,
			string goText)
			: this(true, null, messageType, title, content, goUrl, goText)
		{
		}


		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="allowCloseDialog">是否允许关闭对话框。</param>
		/// <param name="allowCloseWindow">是否允许关闭窗口。为空时将自动判断窗口是否为弹出窗口，如果是则显示关闭窗口按钮。</param>
		/// <param name="messageType">消息的类型。</param>
		/// <param name="title">消息框标题。</param>
		/// <param name="content">消息内容。</param>
		/// <param name="goUrl">页面跳转地址。</param>
		/// <param name="goText">跳转的文本。</param>
		public PageMessage(bool allowCloseDialog,
			bool? allowCloseWindow,
			PageMessageType messageType,
			string title,
			string content,
			string goUrl,
			string goText)
			: this(allowCloseDialog, allowCloseWindow, messageType, title, content, goUrl, goText, null, null, null)
		{
		}


		/// <summary>
		/// 构造函数。
		/// </summary>
		/// <param name="allowCloseDialog">是否允许关闭对话框。</param>
		/// <param name="allowCloseWindow">是否允许关闭窗口。为空时将自动判断窗口是否为弹出窗口，如果是则显示关闭窗口按钮。</param>
		/// <param name="messageType">消息的类型。</param>
		/// <param name="title">消息框标题。</param>
		/// <param name="content">消息内容。</param>
		/// <param name="goUrl">页面跳转地址。</param>
		/// <param name="goText">跳转的文本。</param>
		/// <param name="autoGoUrl">自动跳转的地址。</param>
		/// <param name="autoGoText">自动跳转的文本。</param>
		/// <param name="autoGoDelay">自动跳转的延时时间。PS:单位为毫秒。</param>
		public PageMessage(bool allowCloseDialog,
			bool? allowCloseWindow,
			PageMessageType messageType,
			string title,
			string content,
			string goUrl,
			string goText,
			string autoGoUrl,
			string autoGoText,
			int? autoGoDelay)
		{
			_allowCloseDialog = allowCloseDialog;
			_allowCloseWindow = allowCloseWindow;
			_messageType = messageType;
			_title = title;
			_content = content;
			_goUrl = goUrl;
			_goText = goText;
			_autoGoUrl = autoGoUrl;
			_autoGoText = autoGoText;
			_autoGoDelay = autoGoDelay;
		}


		#region Encode Method
		/// <summary>
		/// 对页面消息进行编码。
		/// </summary>
		/// <param name="messages">页面消息的集合。</param>
		/// <returns>编码后的消息。</returns>
		public static string EncodeMessages(List<PageMessage> messages)
		{
			StringBuilder strMessages = new StringBuilder();

			foreach (PageMessage message in messages)
			{
				StringBuilder strMessage = new StringBuilder();

				strMessage.Append(message.AllowCloseDialog.ToString() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.AllowCloseWindow.ToString() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.MessageType.ToString() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.Title.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.Content.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.GoUrl.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.GoText.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.AutoGoUrl.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.AutoGoText.Escape() + EncodeMessageItemSplitCharacter);
				strMessage.Append(message.AutoGoDelay.ToString());

				if (strMessages.Length > 0)
					strMessages.Append(EncodeMessageSplitCharacter);
				strMessages.Append(strMessage.ToString().Escape());
			}

			return strMessages.ToString();
		}
		#endregion
	}
}
